package panel.viewSpots.navigation
{
	import assists.HtmlText;
	import assists.Scale9Grid;
	import assists.TextFieldUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class BtnClouree extends Sprite
	{
		[Embed(source="../../../assets/public/button.png")]
		private var _btnBitmap:Class;
		
		private var _label:TextField;
		private var _scale9:Scale9Grid;
		public var data:Object;
		private var _onCallBack:Function;
		public function BtnClouree()
		{
			init();
		}
		
		private function init():void
		{
			var bitmap:Bitmap = new _btnBitmap();
			var bitmapData:BitmapData = bitmap.bitmapData;
			_scale9 = new Scale9Grid(bitmapData,10,8);
			this.addChild(_scale9);
			
			_label = new TextField();
			_label.autoSize = "left";
			_label.mouseEnabled = false;
			this.addChild(_label);
		}
		
		public function label(value:String,size:int = 18):void
		{
			_label.text = value;
			TextFieldUtils.setHtmlText(_label,HtmlText.formatSizeColor(value,0,size));
			resize();
		}
		
		private function resize():void
		{
			_label.x = (_scale9.width - _label.width) / 2;
			_label.y = (_scale9.height - _label.height) / 2;
		}
		
		public function autoSize(wid:Number):void
		{
			var scale:Number = wid / this.width;
		    this.width = wid;
			this.height = this.height * scale;
		}
		
		public function set onCallBack(value:Function):void
		{
			_onCallBack = value;
			this.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		protected function clickHandler(event:MouseEvent):void
		{
			_onCallBack();
		}
		
		public function clear():void
		{
			this.removeEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		override public function set width(value:Number):void
		{
			_scale9.width = value;
			resize();
		}
		
		override public function set height(value:Number):void
		{
			_scale9.height = value;
			resize();
		}
		
	}
}